package com.vince.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vince.model.domain.User;
import com.vince.util.DBUtil;

/**
 * 管理员操作
 *  
 *  
 *  分页功能：
 *   定义几个变量：
 *    pagenow 表示第几页
 *    pagecount 总共几页
 *    pagesize 每一页的记录
 *    rowCount 共有多少条记录
 *  
 *  
 * @author susp
 *
 */
@WebServlet("/manage")
public class AdminerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method=request.getParameter("method");
		System.out.println(method);
		User user=(User)request.getSession().getAttribute("user");
		if(user==null){
			//session过期，重定向到登录界面
			response.sendRedirect(request.getContextPath()+"/view/share/login/login.jsp");
			
		}else{//session没过期
			if("manage".equals(method)){
				String sql="select * from user where user_type=1";
				DBUtil mysql=new DBUtil();
				Connection conn=null;
				PreparedStatement ps;
				List<User> userlist=new ArrayList<User>();
				
				try {
					conn=mysql.getConnection();
					ps = conn.prepareStatement(sql);
					ResultSet rs=ps.executeQuery();
					while(rs.next()){
						User user1=new User();
						user1.setUserid(rs.getInt(1));
						user1.setUsertype(rs.getInt(6));
						user1.setUsername(rs.getString(2));
						userlist.add(user1);
					}

				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
			 request.setAttribute("userlist", userlist);
			 request.getRequestDispatcher("/page").forward(request, response);
			}else if("add".equals(method)){
				
			}else if("select".equals(method)){
				
			}else if("exit".equals(method)){
				response.sendRedirect(request.getContextPath()+"/servlet/logoutservlet");
			}
			
		}
		
		
	}

}
